package com.competition.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.competition.entity.ProductCart;
import com.competition.entity.ProductCartDto;
import com.competition.entity.ReceiptInfo;
import com.competition.resp.ResponseResult;
import com.competition.service.ProductCartService;
import com.competition.utils.uuid.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/productCart")
public class ProductCartController {
    @Autowired
    private ProductCartService productCartService;

    @PostMapping("/addProductCart")
    public ResponseResult<Object> addProductCart(@RequestBody HashMap<String,Object> params) {
        System.out.println(params);
        if(params.get("productCartCount")==null || params.get("productId")==null||params.get("productCheck")==null||params.get("userId")==null){
            return new ResponseResult(300,"参数异常",null);
        }
        try {
            Integer productCartCount = Integer.parseInt((String)params.get("productCartCount"));
            String productId = (String) params.get("productId");
            String productCheck = (String) params.get("productCheck");
            String userId = (String) params.get("userId");
            ProductCart productCart = new ProductCart(UUIDUtil.getUUID(),productCartCount,productId,productCheck,userId);

            boolean save = productCartService.save(productCart);
            if (save){
                return new ResponseResult(200,"添加成功",null);
            }else {
                return new ResponseResult(300,"添加失败",null);
            }
        } catch (Exception e) {
            return new ResponseResult(300,"添加失败",null);
        }
    }
    @GetMapping("/getProductCartById")
    public ResponseResult getReceiptInfoById(@RequestParam("userId")String userId){
        if(userId == null){
            return new ResponseResult(400,"空值",null);
        }
        try {
            List<ProductCartDto> list = productCartService.selectAllByUserId(userId);

            return new ResponseResult(200,"获取成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(300,"获取失败",null);
        }
    }
    @PutMapping("/updProductCartById")
    public ResponseResult updProductCartById(@RequestBody Map<String, Object> params){
        if(params.get("productCartId")==null){
            return new ResponseResult(300,"参数异常",null);
        }
        String productCartId = (String) params.get("productCartId");
        Integer productCartCount =null;
        if(params.get("productCartCount")!=null){
            productCartCount = (Integer)params.get("productCartCount");
        }
        String productCheck = (String) params.get("productCheck");

//        String userId = (String) params.get("userId");
//        String productId = (String) params.get("productId");
        try {
            UpdateWrapper<ProductCart> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("product_cart_id",productCartId);
            if(productCartCount!=null ){
                updateWrapper.set("product_cart_count",productCartCount);
            }
            if (productCheck != null && !productCheck.isEmpty()) {
                updateWrapper.set("product_check", productCheck);
            }
            boolean update = productCartService.update(updateWrapper);
            if (update){
                return new ResponseResult(200,"修改成功",null);
            }else {
                return new ResponseResult(400,"修改失败",null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(400,"修改失败",null);
        }

    }


}
